package com.zhiche.lisa.lspm.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.driver.Driver;
import com.zhiche.lisa.lspm.dao.model.fleet.FleetBind;
import com.zhiche.lisa.lspm.dto.PersonFleetBindDTO;
import com.zhiche.lisa.lspm.service.fleet.IFleetBindService;
import com.zhiche.lisa.lspm.vo.fleet.FleetBindVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车队与资源绑定关系 前端控制器
 * </p>
 *
 * @author zhangkun
 * @since 2018-10-31
 */
@RestController
@RequestMapping("/fleetBind")
@Api(value = "/fleetBind", description = "人送司机绑定车队无牵引车绑定车队限制", tags = "人送司机绑定车队无牵引车绑定车队限制")
public class FleetBindController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FleetBindController.class);

    @Autowired
    private IFleetBindService fleetBindService;

    @PostMapping(value = "/addPersonBind")
    @ApiOperation(value = "addPersonBind", notes = "添加人送司机到车队",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FleetBind.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<FleetBind> addLspFleetBindByDriverMobile(@RequestBody PersonFleetBindDTO dto, @RequestHeader("Authorization") String authorization) {
        RestfulResponse<FleetBind> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            fleetBindService.insertPersonBind(dto, authorization);
        } catch (BaseException e) {
            LOGGER.error("FleetBindController.addPersonBind BaseException... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetBindController.addPersonBind ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/queryFleetBindDriver")
    @ApiOperation(value = "/queryFleetBindDriver", notes = "根据车队信息获取所属承运商下的绑定的人送司机", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FleetBind.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<FleetBindVO>> queryFleetBindDriver(@RequestBody Page<FleetBindVO> page, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetBindController/queryFleetBindDriver data: {} ", page);
        RestfulResponse<Page<FleetBindVO>> result = new RestfulResponse<>(0, "成功", null);
        try {
            Page<FleetBindVO> statisticList = fleetBindService.queryFleetBindDriver(page, authorization);
            result.setData(statisticList);
        } catch (BaseException be) {
            LOGGER.error("FleetBindController/queryFleetBindDriver BaseException:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FleetBindController/queryFleetBindDriver error:{}", e.getMessage());
            result.setMessage("操作失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "queryFleetUnBindDriver")
    @ApiOperation(value = "queryFleetUnBindDriver-API", notes = "根据车队信息获取所属承运商下的未绑定人送司机", response = RestfulResponse.class)
    public RestfulResponse<List<Driver>> queryFleetUnBindDriver(@RequestBody FleetBindVO fleetBindVO,
                                                                @RequestHeader("Authorization") String authorization) {
        LOGGER.info("fleetBind/queryFleetUnBindDriver data: {}", fleetBindVO);
        RestfulResponse<List<Driver>> result = new RestfulResponse<>(0, "成功", null);
        try {
            List<Driver> driver = fleetBindService.queryFleetUnBindDriver(fleetBindVO, authorization);
            result.setData(driver);
        } catch (BaseException e) {
            LOGGER.error("FleetBindController/queryFleetBindDriver BaseException:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage("查询异常");
        }
        return result;
    }

    @PostMapping(value = "/removePersonBind")
    @ApiOperation(value = "removePersonBind-API", notes = "移除人送司机绑定", response = RestfulResponse.class)
    public RestfulResponse<List<Driver>> queryFleetUnBindDriver(@RequestBody PersonFleetBindDTO dto,
                                                                @RequestHeader("Authorization") String authorization) {
        RestfulResponse<List<Driver>> result = new RestfulResponse<>(0, "成功", null);
        try {
            fleetBindService.removePersonBind(dto, authorization);
        } catch (BaseException e) {
            LOGGER.error("FleetBindController/removePersonBind BaseException:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage("查询异常");
        }
        return result;
    }
}

